package com.example.broadcastnotifytest618lw;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private Button btnsend,btncheck,btnsenddt;
    private String UNIQUE_STRING = "com.neusoft.broadnotifytest";
    private String UNIQUE_STRING2 = "com.neusoft.broadnotifytest2";

    private MyReceiver2 myReceiver2;

    private View.OnClickListener  listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId())
            {
                case R.id.button://静态
                    Intent intent=new Intent(UNIQUE_STRING);
                    intent.putExtra("para1",editText.getText().toString());
                    intent.setPackage(getPackageName());
                    sendBroadcast(intent);
                    break;
                case R.id.button2://动态
                    Intent intent2=new Intent(UNIQUE_STRING2);
                    intent2.putExtra("para1",editText.getText().toString());
                    intent2.setPackage(getPackageName());
                    sendBroadcast(intent2);
                    break;
                case R.id.button3://清除
                    if(MyReceiver.manager!=null){
                        MyReceiver.manager.cancel(1);

                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myReceiver2= new MyReceiver2();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(UNIQUE_STRING2);
        registerReceiver(myReceiver2,intentFilter);


        init();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver2);

    }

    private void init() {
        editText=findViewById(R.id.editText);

        btncheck=findViewById(R.id.button3);
        btnsend=findViewById(R.id.button);
        btnsenddt=findViewById(R.id.button2);

        btnsend.setOnClickListener(listener);
        btncheck.setOnClickListener(listener);
        btnsenddt.setOnClickListener(listener);


    }
}
